﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace System {
    public static partial class Extention {
        /// <summary>
        /// Reverse 逆序扩展
        /// </summary>
        /// <param name="builder"></param>
        public static void Reverse(this StringBuilder builder) {
            var result = new StringBuilder();
            for(var i = builder.Length - 1;i >= 0;i--) {
                result.Append(builder[i]);
            }
            builder.Remove(0,builder.Length);
            builder.Append(result);
        }
        public static void AppendCollection<T>(this StringBuilder builder,IEnumerable<T> list,Func<T,string> func) {
            foreach(var item in list) {
                builder.AppendLine(func(item));
            }
        }
        /// <summary>
        /// <code>
        ///  List<Person> people = ...
        ///  StringBuilder builder = new StringBuilder();
        ///  builder.AppendCollection(people );
        ///  var s = builder.ToString();
        /// </code>
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="collection"></param>
        public static void AppendCollection<T>(this StringBuilder sb, List<T> col,Func<T,string> printer) {
            col.ForEach(o => sb.AppendLine(printer(o)));
        }
        #region 删除字符
        /// <summary>
        /// 删除最后结尾的一个逗号
        /// </summary>
        public static void DelLastComma(this StringBuilder builder) {
            builder.Remove(builder.ToString().LastIndexOf(','),1);
        }
        /// <summary>
        /// 删除最后结尾的指定字符后的字符
        /// </summary>
        public static StringBuilder DelLastChar(this StringBuilder builder,string strchar) {
            string strtemp = builder.ToString();
            int n = strtemp.LastIndexOf(strchar);
            if(n > 0) {
                builder = new StringBuilder();
                builder.Append(strtemp.Substring(0,n));
            }
            return builder;
        }
        /// <summary>
        /// 删除指定位置的字符
        /// </summary>
        /// <param name="Start">开始索引</param>
        /// <param name="Num">删除个数</param>
        public static void Remove(this StringBuilder builder,int Start,int Num) {
            builder.Remove(Start,Num);
        }
        public static void Clear(this StringBuilder builder) {
            builder.Remove(0,builder.Length);
        }
        #endregion
    }
}